package com.homework.dao;

import java.io.BufferedWriter;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.homework.util.PublicMethod;
import com.homework.util.connDB;
import com.sina.sae.storage.SaeStorage;

public class EditorDao {
	private static EditorDao editorDao = null;
	private connDB conn = new connDB();
	private ResultSet rs;
	private static String path = PublicMethod.getVicPath();
	private static String pathEva = PublicMethod.getEvaPath();
	public static EditorDao getInstance() {
		if (editorDao == null) {
			editorDao = new EditorDao();
		}
		return editorDao;
	}

	private String setDir(){
		return "d:/test11112.txt";
	}
	
	private String getDir(String homeworkId){
		String dir = "d:/test11112.txt";	
		try {
			String sql = "select f_homeworkVirt from t_homework where f_homeworkId=" + homeworkId;
			rs = conn.executeQuery(sql);
			if (rs.next()) {
				dir = (rs.getString("f_homeworkVirt"));
				rs.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			conn.close();
		}
		return dir;
	}
	
	private String setDir(int chooseId){	
		String dir = null;
		try {
			int num = 0;
			String sql = "select f_homeworkNum from t_choose where f_chooseId=" + chooseId;
			rs = conn.executeQuery(sql);
			if (rs.next()) {
				num = (rs.getInt("f_homeworkNum"));
				rs.close();
			}
			num++;
			String sql1 = "update t_choose set f_homeworkNum=" + num + " where f_chooseId=" + chooseId;
			conn.executeUpdate(sql1);
			
			
			dir = String.format("%05d", chooseId) + String.format("%05d", num) + ".txt";
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			conn.close();
		}
		return dir;
	}

	public int[] getBatchChoose(String homeworkIdT){
		String sql = "select f_batch,f_chooseId from t_homework where f_homeworkId=" + homeworkIdT;
		ResultSet rs = conn.executeQuery(sql);
		int a[] = new int[2];
		try {
			if (rs.next()) {
				a[0] = rs.getInt("f_batch");
				a[1] = rs.getInt("f_chooseId");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return a;
	}
	
	public boolean saveHomeworkT(String json,int batch,int chooseId)
	{
		
		BufferedWriter bw = null;
		BufferedWriter bw1 = null;
		String dir = null;
		try {
			dir = setDir(chooseId);
			SaeStorage ss = new SaeStorage(connDB.getAccessKey(),connDB.getSecretKey(),connDB.getAppName());
			ss.write("homework",path + dir,json);
			ss.write("homework",pathEva + dir,"");
			
			String sql = "insert into t_homework(f_batch,f_homeworkVirt,f_track,f_chooseId,f_homeworkStatus) values ("
							+ batch 
							+",'" 
							+path + dir+ "','" 
							+pathEva + dir+ "'," 
							+ chooseId+",1)";
			int result = conn.executeUpdate(sql);

			if (result == 1) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			conn.close();
		}
		return false;
	}
	
	public boolean updateHomework(String json,String homeworkId)
	{
		BufferedWriter bw = null;
		String dir = null;
		try {
			
			String sql = "select f_homeworkVirt from t_homework where f_homeworkId = "+ homeworkId;
			ResultSet rs = conn.executeQuery(sql);

			if(rs.next())
			{
				dir = rs.getString("f_homeworkVirt");
				if(dir != null&&dir.trim() != "")
				{
					SaeStorage ss = new SaeStorage(connDB.getAccessKey(),connDB.getSecretKey(),connDB.getAppName());
					ss.write("homework",dir,json);
					return true;
				}
				else
				{
					return false;
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			conn.close();
			try {
				bw.close();
				//br.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return false;
	}

	

	public String getHomework(String homeworkId){
		String adr = getDir(homeworkId);
		SaeStorage ss = new SaeStorage(connDB.getAccessKey(),connDB.getSecretKey(),connDB.getAppName());
		byte[] tmp = ss.read("homework", adr);
		return new String(tmp);
	}

	public int getStatus(String homeworkId) {
		// TODO Auto-generated method stub
		int status = 1;	
		try {
			String sql = "select f_homeworkStatus from t_homework where f_homeworkId=" + homeworkId;
			rs = conn.executeQuery(sql);
			if (rs.next()) {
				status = (rs.getInt("f_homeworkStatus"));
				rs.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			conn.close();
		}
		return status;
	}

	public int getChooseStu(int i, Object stuId) {
		// TODO Auto-generated method stub
		int retCourse = 0;
		
		String sql = "select f_courseId from t_choose where f_chooseId=" + i;
		ResultSet rs = conn.executeQuery(sql);
		int a[] = new int[2];
		try {
			if (rs.next()) {
				a[0] = rs.getInt("f_courseId");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			conn.close();
		}
		
		if(a[0] != 0){
			
			String sql1 = "select f_chooseId from t_choose where f_courseId=" + a[0] + " and f_userId=" + stuId;
			ResultSet rs1 = conn.executeQuery(sql1);
			try {
				if (rs1.next()) {
					retCourse = rs1.getInt("f_chooseId");
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
				conn.close();
			}
		}
		
		return retCourse;
	}
	
	public static void main(String args[]){
		EditorDao dao = new EditorDao();
		dao.setDir(2);
	}

	public void updateHomeworkState(String homeworkIdT) {
		// TODO Auto-generated method stub
		String sql = "update t_homework set f_homeworkStatus=1 where f_homeworkId = "+homeworkIdT;
		conn.executeUpdate(sql);
		conn.close();
	}
}
